/*****************************************************************************************************************
  * 用途：jShare基础框架
  * 作者：高宇舟
  * 修改日志：
  *    日期         修改原因            修改人              版本
  * 2012.06.08        创建             高宇舟           @version 1.0
******************************************************************************************************************/
(function(wnd,unf){
    var doc = wnd.document;
	var url = document.location.pathname;
	var tmpUrl = url.indexOf('/', 1);
	var webPath = tmpUrl < 0 ? url : url.substr(0, tmpUrl);
	if (webPath.indexOf('/') == -1) {
        webPath = '/' + webPath;
	}
	
	(function(arr) {
        for(var i=0,len=arr.length; i<len; i++){
			if(/jShare.js/.test(arr[i].src)){
  				jShare.root = arr[i].src.replace('/jShare.js',''); 
  				break;
  			}
    	}
	})(doc.getElementsByTagName('script'),wnd.jShare = {version:'1.0'});
	
	var _BROWSER = navigator.userAgent.toLowerCase();

	var $Browser = jShare.Browser = {
			 ie: /msie/.test(_BROWSER) && !/opera/.test(_BROWSER),
			 mf: /mozilla/.test(_BROWSER) && !/(compatible|webkit)/.test(_BROWSER),
		  opera: /opera/.test(_BROWSER),
		 safari: /webkit/.test(_BROWSER),
		edition: (_BROWSER.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [0,'0'])[1]
	};
	
	var $Match = jShare.Match = {
		equal: function(a,b) {
					return a == b && a === b;
			   },
		isEmpty: function(obj) {
				     return !(obj === 0) ? !obj || (($Match.isString(obj) || $Match.isArray(obj)) && !obj.length) : false;
				 },
		isObject: function(obj) {
					 return !!obj && $Parse.toStr(obj) === '[object Object]';
				 },
		isFunction: function(fun) {
					 return !!fun && $Parse.toStr(fun) === '[object Function]';
					},
		isArray: function(arr) {
				     return !!arr && $Parse.toStr(arr) === '[object Array]';
				 },
		isString: function(str) {
				      return $Parse.toStr(str) === '[object String]';
				  },
		isNum: function(num) {
		           return !isNaN(num) && /^-?\d+((\.\d+)|(e-?\d+))?$/.test(num);
               },
		isInt: function(num) {
		   	       return !isNaN(num) && /^-?[1-9]\d*$/.test(num);
		   	   },
		isFlt: function(flt) {
		           return !isNaN(num) && /^-?\d+\.\d+$/.test(flt);
		       },
		isCode: function(str) {
				    return /^[_A-Z]\w*$/i.test(str);
				},
		isBoolean: function(obj) {
				       return $Parse.toStr(obj) === '[object Boolean]';
				   },
		isDate: function(obj) {
		            return !!obj && $Parse.toStr(obj) === '[object Date]';
				},
		isDefined: function(obj) {
				       return typeof obj !== 'undefined';
				   },
		isElement: function(obj) {
				       return !!(obj || 0).tagName;
				   },
		isXml: function(xml) {
			       var obj = null;
				   try{
				       obj = (xml ? xml.ownerDocument || xml : 0).documentElement;
				   }catch(e){
				       obj = null;
				   }finally{
				       return obj ? obj.nodeName !== 'HTML' : false;
				   }
			   },
		isDom: function(obj) {
			       return !!obj && /^1|9$/.test(obj.nodeType);
			   },
		isUrl: function(url) {
			       return /^(((ht|f)tps?:\/)?\/|(\.+\/)*)[\w\-]+([:\.\/][\w\-]+)*\/?(\?(\w+(=[^\s]*)?&?)+)?(#\w*)?$/i.test(url);
			   },
		isJsn: function(jsn) {
			       return $Match.isString(jsn) ? /^[\],:{}\s]*$/.test(
				              jsn.replace(
							      /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@'
							  ).replace(
							      /"[^"\\\n\r]*"|NaN|true|false|null|undefined|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'
							  ).replace(
							      /(?:^|:|,)(?:\s*\[)+/g, ''
							  )
						   ) : $Match.isObject(jsn) && !$Match.isDom(jsn) && !$Match.isDefined(jsn.length);
				   },
		
		isCard: function(str) {
				    return /^[1-9]([0-9]{14}|[0-9]{16}[0-9X])$/i.test(str);
				},
		isEmail: function(str) {
			         return /^\w+((-\w+)|(\.\w+))*\@[A-Z0-9]+((\.|-)[A-Z0-9]+)*\.[A-Z0-9]+$/i.test(str);
				 }
	};
	
	var $Parse = jShare.Parse = {
	    toNum: function(src) {
			       return $Match.isNum(src) ? Number(src) : function(str){
						      try{
							    return parseInt(str);
						      }catch(e){return 0}
					      }(src);
				},
		toInt: function(src,cep) {
			       return $Match.isNum(cep) ? parseInt(src,cep || 10) : parseInt($Parse.toNum(src),cep || 10);
	      	   },
	    toFlt: function(src,cep) {
	      	       return $Match.isNum(cep) ? $Math.fix(parseFloat(src),cep || 2) : $Math.fix(parseFloat($Parse.toNum(src),cep || 2));
	      	   },
	    toStr: function(src) {
	      	       return Object.prototype.toString.call(src);
	      	   },
	    toTxt: function(src) {
	      	       return $Match.isDefined(src) ? src + '' : '';
	      	   }
	};
	
	var $Math = jShare.Math = {
	    fix: function(flt,cep) {
		         return Number.prototype.toFixed.call(flt,cep);
		     }
	};
	
	var $Util = jShare.Util = {
	    getWebPath: function() {
	    	            return window.location.protocol + '//' + window.location.host + webPath;
	                },
	    getTimeDiff: function(timeOne, timeTwo) {
	      			    if (timeOne && timeTwo) {
						    var beginTime = Date.parse(timeOne.replace(/-/g, "/"));
				            var endTime = Date.parse(timeTwo.replace(/-/g, "/"));
				            var days = (endTime - beginTime)/(1000*60*60*24);
				            return Math.ceil(days);
			            } else {
				            return null;
			            }
	                 },
	    getTimeByInterval: function(num) {
			var myDate = new Date(); 
			var year = myDate.getYear(); 
			var month = myDate.getMonth(); 
			var date = myDate.getDate(); 
			var uom = new Date(year,month,date); 
			uom.setDate(uom.getDate()+num);	// 取得系统时间的前一天,负数是前几天,正数是后几天 
			var iMM = uom.getMonth(); 
			iMM++;
			var sMM = iMM >= 10 ? iMM : ('0'+iMM); 
			var iDD = uom.getDate(); 
			var sDD = iDD >= 10 ? iDD : ('0'+iDD); 
			uom = uom.getFullYear() + '-' + sMM + '-' + sDD + ' ' + myDate.toLocaleTimeString(); 
			return uom; 
		},
		toChar:function(datetime, format) {
			if(datetime=="" || datetime==null || datetime==undefined){
				return "";
			}else{
				var date = new Date(datetime);
				var yyyy = date.getFullYear();
				var mm = date.getMonth()+1;
				var dd = date.getDate();
				var hh24 = date.getHours();
				var mi = date.getMinutes();
				var ss = date.getSeconds();
				var s1 = format.replace(/yyyy|YYYY/g, yyyy);
				var s2 = s1.replace(/mm|MM/g,mm<10 ? "0" + mm : mm);
				var s3 = s2.replace(/dd|DD/g,dd<10 ? "0" + dd : dd);
				var s4 = s3.replace(/hh24|HH24/g,hh24<10 ? "0" + hh24 : hh24);
				var s5 = s4.replace(/mi|MI/g,mi<10 ? "0" + mi : mi);
				var s6 = s5.replace(/ss|SS/g,ss<10 ? "0" + ss : ss);
				return s6;
			}		
		},
		ellipsis: function(str, maxlength, suffix) {
		    if (str.length <= maxlength) {
				return str;
			} else {
				return str.substr(0, maxlength - 0) + suffix
			}
		},
		getQueryString: function(url, name) {
			var reg = new RegExp('(^|\\?|&)'+ name +'=([^&]*)(\\s|&|$)', 'i');
			if (reg.test(url)){
				return RegExp.$2.replace(/\+/g, ' ');
			}
			return '';
		}
	};
	
	var $Product = jShare.Product = {
		click : function(id){
			var url  = '/json?action=UPDATE_CLICK_COUNT_ACTION&PRODUCT_ID='+id;
			$.post(url,{},function(data){},'json');
		}
	};
	
	var $Base = jShare.Base = {
		
		//未登录导航到登陆页面
		isLogin: function(to_url, fun){
			var url = '/json?action=GET_MEMBER_SESSION_ACTION';
			$.post(url,{},function(data){
				if(data.success){
					var obj  = data.root.MEMBER_SESSION;
					if(obj == undefined){
						location.href = "/user/login.html?to_url="+to_url;
						return;
					}
					if(fun){
						fun();
					}
				}
			},'json');	
		},

		directPage: function(to_url){
			var url = '/json?action=GET_MEMBER_SESSION_ACTION';
			$.post(url,{},function(data){
				if(data.success){
					var obj  = data.root.MEMBER_SESSION;
					if(obj == undefined){
						location.href = "/user/login.html?to_url="+to_url;
					}
					else{
						location.href = to_url;
					}
				}
			},'json');	
		},
		
		isLoginManage: function(fun, params){
			var url = '/json?action=GET_USER_SESSION_ACTION';
			$.post(url,{},function(data){
				if(data.success){
					var obj = data.root.USER_SESSION;
					if(obj == undefined){
						$("#login_window").zxxbox({
							title : '超时登录',
							width : 400,
							height : 200,
							opacity : 1,
							fix : true,
							show : function(){
								$('#LOGIN_NAME').val('');
								$('#PASSWORD').val('');
							},
							onclose : function(){
								$.ajaxSetup({async: false});
								$.post(url,{},function(data){
									if(data.success){
										var obj = data.root.USER_SESSION;
										if(obj == undefined){
											location.href = "/admin/index.html";
										}
										else if(fun){
											fun(params);
										}
									}
								},'json');
								$('#LOGIN_NAME').val('');
								$('#PASSWORD').val('');
								$.ajaxSetup({async: true});
							}
						});
					}
					else if(fun){
						fun(params);
					}
				}
			},'json');	
		},
		
		lockSrceen : function(){
			$.post('/json?action=LOGOUT_ACTION',{},function(data){
				if(data.success){
					$("#login_window").zxxbox({
						title : '锁屏登录',
						width : 400,
						height : 200,
						opacity : 1,
						fix : true,
						show : function(){
							$('#LOGIN_NAME').val('');
							$('#PASSWORD').val('');
						},
						onclose : function(){
							$.ajaxSetup({async: false});
							$.post('/json?action=GET_USER_SESSION_ACTION',{},function(data){
								if(data.success){
									var obj = data.root.USER_SESSION;
									if(obj == undefined){
										location.href = "/admin/index.html";
									}
									else{
										fun(params);
									}
								}
							},'json');
							$('#LOGIN_NAME').val('');
							$('#PASSWORD').val('');
							$.ajaxSetup({async: true});
						}
					});
				}
			},'json');
		},

		//'1 网银支付 2 在线第三方支付 3 货到付款 4 银行汇款',
		getPayMethod: function(id){
			var str = '';
			switch(id){
				case 1 : 
					str = '网银支付';
					break;
				case 2 : 
					str = '在线第三方支付';
					break;
				case 3 : 
					str = '货到付款';
					break;
				case 4 : 
					str = '银行汇款';
					break;
			}
			return str;
		},
		//'1 快递 2 EMS 3 平邮 4 物流 5 上门自提',
		getDeliverMethod: function(id){
			var str = '';
			switch(id){
				case 1 : 
					str = '快递';
					break;
				case 2 :
					str = 'EMS';
					break;
				case 3 :
					str = '平邮';
					break;
				case 4 :
					str = '物流';
					break;
				case 5 :
					str = '上门自提';
					break;
			}
			return str;
		},
		//'1 时间不限 2 周一到周五 3 周六日及公众节假日',
		getDeliverTime: function(id){
			var str = '';
			switch(id){
				case 1 : 
					str = '时间不限';
					break;
				case 2 :
					str = '周一到周五';
					break;
				case 3 :
					str = '周六日及公众节假日';
					break;
			}
			return str;
		},
		//积分类型
		getPointItem: function(id){
			var str = '';
			switch(id){
				case '10' : 
					str = '交易获得';
					break;
				case '11' :
					str = '兑换';
					break;
				case '20':
					str = '兑换';
					break;
				case '21':
					str = '到期';
					break;
				case '30':
					str = '退货';
					break;
			}
			return str;
		},
		//审核状态
		getVerifyStatus: function(id){
			var str = '';
			switch(id){
				case 0 : 
					str = '未审核';
					break;
				case 1 :
					str = '已通过';
					break;
				case 2 :
					str = '未通过';
					break;
			}
			return str;
		},
		//订单状态
		getOrderStatus: function(id){
			var str = '';
			switch(id){
				case 1 : 
					str = '未付款';
					break;
				case 2 :
					str = '已付款';
					break;
				case 3 :
					str = '等待处理';
					break;
				case 4 :
					str = '配货中';
					break;
				case 5 : 
					str = '已出库';
					break;
				case 6 :
					str = '已发货';
					break;
				case 7 :
					str = '交易完成';
					break;
				case 8 :
					str = '交易取消';
					break;
				case 9 :
					str = '交易异常';
					break;
				case 10 :
					str = '申请退款';
					break;
				case 11 :
					str = '已退款';
					break;
			}
			return str;
		}
	};
})(window);